package com.boot.controller;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @GetMapping("/login")
    public String toLogin(Map<String, Object> map) {
        return "login";
    }

    @PostMapping("/user/login")
    public String doLogin(@RequestParam("username") String username, @RequestParam("password") String password, Map<String,Object> map, HttpSession session) {
        logger.info("username:{},password:{}",username,password);
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            session.setAttribute("loginUser", username);
            return "redirect:/main.html";
        }else{
            map.put("errMsg", "用户名或密码错误");
            return "login";
        }
    }
}
